/*
 * TCP Socket for sending data only.
 */
package org.dbdir.connections;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import org.dbdir.core.Logger;

/**
 *
 * @author thotheolh
 */
public class TCPSocket {

    private String IPAddr;
    private int portNum;
    private int timeOut = 60;
    private PrintWriter out = null;
    private BufferedReader in = null;
    private Socket soc = null;
    private Logger logger;

    public TCPSocket(Logger logger, String ipaddr, int portnum) {
        this.logger = logger;
        if (logger == null) {
            logger = new Logger();
        }
        try {
            setIPAddr(ipaddr);
            setPortNum(portnum);
            soc = new Socket(getIPAddr(), getPortNum());
            out = new PrintWriter(getSoc().getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(getSoc().getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        } catch (UnknownHostException ex) {
            logger.log(2, ex.getMessage());
        } catch (IOException ex) {
            logger.log(2, ex.getMessage());
        }
    }

    public void output(String message) {
        getOut().println(message);
    }

    public boolean isConnected() {
        boolean ok = false;
        try {
            ok = InetAddress.getByName(this.getIPAddr()).isReachable(timeOut);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(TCPSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ok;
    }

    public void close() {
        try {
            getSoc().close();
            getOut().close();
            getIn().close();
        } catch (IOException ex) {
            logger.log(2, ex.getMessage());
        }
    }

    /**
     * @return the IPAddr
     */
    public String getIPAddr() {
        return IPAddr;
    }

    /**
     * @param IPAddr the IPAddr to set
     */
    public void setIPAddr(String IPAddr) {
        this.IPAddr = IPAddr;
    }

    /**
     * @return the portNum
     */
    public int getPortNum() {
        return portNum;
    }

    /**
     * @param portNum the portNum to set
     */
    public void setPortNum(int portNum) {
        this.portNum = portNum;
    }

    /**
     * @return the out
     */
    public PrintWriter getOut() {
        return out;
    }

    /**
     * @param out the out to set
     */
    public void setOut(PrintWriter out) {
        this.out = out;
    }

    /**
     * @return the in
     */
    public BufferedReader getIn() {
        return in;
    }

    /**
     * @param in the in to set
     */
    public void setIn(BufferedReader in) {
        this.in = in;
    }

    /**
     * @return the soc
     */
    public Socket getSoc() {
        return soc;
    }

    /**
     * @param soc the soc to set
     */
    public void setSoc(Socket soc) {
        this.soc = soc;
    }
}
